package com.linewell.dao;

import com.linewell.entity.Article;
import com.linewell.entity.ArticleVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Title: ssm2
 * @description:
 * @author: hyuhuang@linewell.com
 * @since:2018/1/17 13:42
 */
public interface ArticleMapper {
    @ResultMap("com.linewell.dao.ArticleMapper.Article")
    @Select("select a.*,u.*,t.* from article a,userinfo u,tag t,multilabel m where lable_id=article_id and a.user_id=u.user_id and t.tag_id=m.tag_id and m.type=2 and a.user_id = ${userId}")
    List<Article> selectArticleWithUserId(@Param("userId") Long userId);

    @ResultMap("com.linewell.dao.ArticleMapper.Article")
    @Select("select top 6 a.*,u.*,t.* from article a,userinfo u,tag t,multilabel m where lable_id=article_id and acheck = 1 and a.user_id=u.user_id and t.tag_id=m.tag_id and m.type=2 order by newid()")
    List<Article> selectAllArticle();

    @Insert("insert into article(article_id,title,user_id,zan_num,cai_num,acheck,content,article_date,article_picture,sim_content,collect_num) values(#{articleId},#{title},${userInfo.userId},0,0,0,#{content},#{articleDate},#{articlePicture},#{simContent},0)")
    boolean addArticle(Article article);

    @Update("update article set zan_num = ${zanNum} where article_id = ${articleId}")
    boolean updataArticleZan(@Param("articleId") Long articleId , @Param("zanNum") int zanNum);

    @ResultMap("com.linewell.dao.ArticleMapper.Article")
    @Select("SELECT a.*,u.*,t.* " +
            "FROM article a,userinfo u,tag t,multilabel m " +
            "WHERE lable_id = article_id and a.user_id = u.user_id and t.tag_id=m.tag_id and m.type=2 and article_id=${articleId};")
    Article selectArticleWithArticleId(@Param("articleId") Long articleId);

    @Select("SELECT user_id FROM article where  article_id = ${articleId}")
    Long getUserName(@Param("articleId") long articleId);

    @ResultMap("com.linewell.dao.ArticleMapper.ArticleMap")
    @Select("SELECT top 5 a.* " +
            "FROM article a,collected c " +
            "WHERE c.article_id=a.article_id AND a.acheck=1 AND a.user_id=${userId} ORDER BY(collect_num) DESC")
    List<Article> selectById(@Param("userId")long id);

    @ResultMap("com.linewell.dao.ArticleMapper.Article")
    @Select("SELECT a.*,u.*,t.* FROM article a,multilabel m,tag t,userinfo u " +
            "WHERE m.type = 2 AND t.tag_id = m.tag_id AND m.lable_id = a.article_id " +
            "AND u.user_id = a.user_id AND t.tag_id = ${tagId} order by newid() Offset (${page}-1)*5 Row Fetch Next 5 Rows Only")
    List<Article> selectByTagId(@Param("tagId") Long tagId,@Param("page") int page);

    @ResultMap("com.linewell.dao.ArticleMapper.Article")
    @Select("select a.*,u.*,t.* " +
            "from article a,userinfo u,tag t,multilabel m " +
            "where lable_id=article_id " +
            "and a.user_id=u.user_id " +
            "and t.tag_id=m.tag_id " +
            "and a.acheck = 1" +
            "and m.type=2 " +
            "order by (${sort}) DESC " +
            "Offset (${page}-1)*10 Row Fetch Next 10 Rows Only")
    List<Article> selectBySort(@Param("page") Integer page,@Param("sort") String sort);

    @ResultMap("com.linewell.dao.ArticleMapper.Article")
    @Select("select a.*,u.*,t.* " +
            "from article a,userinfo u,tag t,multilabel m " +
            "where lable_id=article_id " +
            "and a.user_id=u.user_id " +
            "and t.tag_id=m.tag_id " +
            "and m.type=2 " +
            "and a.acheck =1 " +
            "and a.${searchType} like '%${keyword}%'" +
            "order by article_date DESC " +
            "Offset (${page}-1)*5 Row Fetch Next 5 Rows Only")
    List<Article> searchArticle(@Param("searchType") String searchType,@Param("keyword") String keyword,@Param("page") Integer page);

    @Select("select * from article where article_id = ${articleId}")
    Article selectArticleByArticleId(@Param("articleId")Long articleId);

    @ResultMap("com.linewell.dao.ArticleMapper.Article")
    @Select("select a.*,u.*,t.*  " +
            "from article a,userinfo u,tag t,multilabel m  " +
            "where lable_id=article_id  " +
            "and a.user_id=u.user_id  " +
            "and t.tag_id=m.tag_id  " +
            "and m.type=2 " +
            "and a.acheck =1" +
            "order by (zan_num) DESC Offset (${page}-1)*10 Row Fetch Next 10 Rows Only")
    List<Article> getBestRank(@Param("page") int page);

    @ResultMap("com.linewell.dao.ArticleMapper.ArticleDatagrid")
    @Select("select article_id,title,nick_name user_name,acheck,content,article_date,article_picture,sim_content  " +
            "from article,userinfo " +
            "where article.user_id = userinfo.user_id " +
            "and article.content like '%好%' ")
    List<ArticleVo> searchArticleKey(@Param("articleKey")String articleKey);

    @ResultMap("com.linewell.dao.ArticleMapper.ArticleDatagrid")
    @Select("select article_id,title,nick_name user_name,acheck,content,article_date,article_picture,sim_content  " +
            "from article,userinfo " +
            "where article.user_id = userinfo.user_id " +
            "order by article_date Desc Offset (${page}-1)*${limit} Row Fetch Next ${limit} Rows Only")
    List<ArticleVo> searchAllArticle(@Param("page") int page,@Param("limit") int limit);

    @Update("update article set acheck = ${acheck} where article_id = ${articleId}")
    boolean updataArticleCheck(@Param("articleId")Long articleId,@Param("acheck") int acheck);

    @Delete("DELETE from article where article_id = ${articleId}")
    boolean deleteArticleByArticleId(@Param("articleId") Long articleId);

    @Select("select count(*) from article ")
    int getCount();

    @Update("update article set ${field} = #{value} where article_id = ${articleId}")
    boolean updateArticle(@Param("field")String field, @Param("value")String value,@Param("articleId")Long articleId);

    @Delete("delete from article where article_id in (${articleIds})")
    boolean deleteAllArticle(@Param("articleIds")String articleIds);

    @Select("select count(*) from article where acheck = 0")
    int getCountIsCheck();

    @Select("select article_id,title,nick_name user_name,acheck,content,article_date,article_picture,sim_content  " +
            "from article,userinfo " +
            "where article.user_id = userinfo.user_id  and acheck = 0" +
            "order by article_date Desc Offset (${page}-1)*${limit} Row Fetch Next ${limit} Rows Only")
    List<ArticleVo> searchAllArticleIsCheck(@Param("page")int page, @Param("limit")int limit);

    @Update("update article set title = #{title}," +
            "acheck = 0,content = #{content},article_date = #{articleDate}," +
            "sim_content = #{simContent},article_picture = #{articlePicture} where article_id = ${articleId}")
    boolean updateArticle1(Article article);
}
